from flask import Blueprint, request, jsonify, redirect
import json
import datetime
import random
from db import db1

bp_wuliu = Blueprint('wuliu', __name__)


@bp_wuliu.route('/wuliu', methods=['GET'])
def wuliu():
    if request.method == 'GET':
        name = request.args.get('name')
        faddress = request.args.get('faddress')
        maddress = request.args.get('maddress')
        mobile = request.args.get('mobile')
        gongsi = request.args.get('gongsi')
        order_no = datetime.datetime.strftime(datetime.datetime.now(), "%Y%m%d%H%M%S") + str(
            random.randint(10000, 99999))
        sql = 'select money from address where faddress = "%s" and maddress = "%s"' % (faddress, maddress)
        res = db1.findone(sql)
        sql2 = 'insert into wuliu_order(order_no,name,mobile,faddress,maddress,gongsi,money) values("%s","%s","%s","%s","%s","%s",%d)' % (order_no, name, mobile, faddress, maddress, gongsi, int(res['money']))
        db1.update(sql2)
        return redirect('http://127.0.0.1:8080/#/show_wuliu')


@bp_wuliu.route('/order', methods=['GET'])
def order():
    if request.method == 'GET':
        sql = 'select * from wuliu_order'
        res = db1.findall(sql)
        return jsonify({'code': 200, 'list': res})
